package com.newtcdemo.CustomViews.todayTopThreeChart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.newtcdemo.R;
import com.newtcdemo.utils.DisplayUtil;

import java.util.List;

/**
 * Created by admin on 2017/4/18.
 * 今日前三折线
 */
public class TodayTopThreeBrokenLine extends View {

    private List<Float> mMilliliter;//存储各个竖线的高度
    private float mYInterval;//Y轴的间隔
    private float mWidth;//左边到右边的间隔，即圆柱的宽度
    private float mIntervalLeftRight;//左边到右边的间隔，即圆柱的宽度
    private float mIntervalLeft;//屏幕距离左边的间隔，即第一条竖线到左边的距离
    private Paint mPaintBrokenLine;//折线画笔
    private Paint mPaintCircle;
    private Path mPath;//几何路径
    private float mMaxValue = 0;//最高的折线
    private int mShowIndex=6;
    private Context mContext;

    public TodayTopThreeBrokenLine(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TodayTopThreeBrokenLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TodayTopThreeBrokenLine(Context context) {
        super(context);
        mContext=context;
        mYInterval = DisplayUtil.dip2px(context,7.5f);
        //画折线用的画笔
        mPaintBrokenLine = new Paint();
        mPaintBrokenLine.setStrokeWidth(DisplayUtil.dip2px(context,3.5f));
        mPaintBrokenLine.setStyle(Paint.Style.STROKE);
        mPaintBrokenLine.setColor(getResources().getColor(R.color.orange_line));
        mPaintBrokenLine.setAntiAlias(true);
        //圆点
        mPaintCircle = new Paint();
        mPaintCircle.setStrokeWidth(DisplayUtil.dip2px(context,5f));
        mPaintCircle.setStyle(Paint.Style.FILL);
        mPaintCircle.setColor(getResources().getColor(R.color.orange_line));
        mPaintCircle.setAntiAlias(true);
        //画笔路径
        mPath = new Path();
        //设置覆盖整个视图的屏幕
        setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void init(int width, int size, float maxValue) {
        this.mMaxValue = maxValue;
        mWidth=width;
        mIntervalLeftRight = width / size;
        mIntervalLeft = mIntervalLeftRight * 0.5f;
    }

    public void setList(List milliliter) {
        if (null == milliliter || milliliter.size() == 0)
            return;
        this.mMilliliter = milliliter;
        postInvalidate(); //重新绘制
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.getParent().requestDisallowInterceptTouchEvent(true);//当该view获得点击事件，就请求父控件不拦截事件
        float eventX = event.getX();
        mShowIndex=0;
        Float[] floatsX = new Float[]{mWidth/ 14f, mWidth / 14f * 3, mWidth / 14f * 5,
                mWidth / 14f * 7, mWidth / 14f * 9, mWidth / 14f * 11, mWidth / 14f * 13};
        for (int i = 0; i < floatsX.length; i++) {
            if (Math.abs(eventX - mWidth) < mWidth) {
                if (eventX >= floatsX[i] && eventX < (floatsX[i]+(mWidth/ 7f))) {
                    if (Math.abs(eventX - floatsX[i]) <= (mWidth/ 14f)) {
                        mShowIndex = i;
                    } else {
                        mShowIndex = i + 1;
                    }
                }
            }
        }
        postInvalidate();
        return super.onTouchEvent(event);
    }

    public int getShowIndex(){
        return mShowIndex;
    }

    public void setShowIndex(int index){
        mShowIndex=index;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == mMilliliter || mMilliliter.size() == 0 ) return;
        drawBrokenLine(canvas);
    }

    //画折线
    private void drawBrokenLine(Canvas canvas) {
        mPath.reset();//清除路径中的任何线条和曲线，使其为空。
        if (mMaxValue<=0){
            float maxBrokenLineAvgHeight = getHeight() - mYInterval*2;//从最高数取得折线平均等分
            for (int i = 0; i < mMilliliter.size() - 1; i++) {
                float startX = mIntervalLeftRight * i + mIntervalLeft;
                float stopX = mIntervalLeftRight * (i + 1) + mIntervalLeft;
                float startY=getHeight()- (maxBrokenLineAvgHeight * mMilliliter.get(i))-mYInterval;
                float stopY=getHeight()- (maxBrokenLineAvgHeight * mMilliliter.get(i + 1))-mYInterval;
                if (i == 0) {
                    mPath.moveTo(startX, startY);
                }
                mPath.lineTo(startX, startY);
                mPath.lineTo(stopX, stopY);
                canvas.drawPath(mPath, mPaintBrokenLine);
            }
            for (int i = 0; i < mMilliliter.size(); i++) {
                float startX = mIntervalLeftRight * i + mIntervalLeft;
                float startY = getHeight()- (maxBrokenLineAvgHeight * mMilliliter.get(i))-mYInterval;
                if (i==mShowIndex){
                    canvas.drawCircle(startX,startY, DisplayUtil.dip2px(mContext, 5f),mPaintCircle);
                }
            }
        }else {
            float maxBrokenLineAvgHeight = (getHeight() - mYInterval*2) / mMaxValue;//从最高数取得折线平均等分
            for (int i = 0; i < mMilliliter.size() - 1; i++) {
                float startX = mIntervalLeftRight * i + mIntervalLeft;
                float stopX = mIntervalLeftRight * (i + 1) + mIntervalLeft;
                float startY=getHeight()- (maxBrokenLineAvgHeight * mMilliliter.get(i))-mYInterval;
                float stopY=getHeight()- (maxBrokenLineAvgHeight * mMilliliter.get(i + 1))-mYInterval;
                if (i == 0) {
                    mPath.moveTo(startX, startY);
                }
                mPath.lineTo(startX, startY);
                mPath.lineTo(stopX, stopY);
                canvas.drawPath(mPath, mPaintBrokenLine);
            }
            for (int i = 0; i < mMilliliter.size(); i++) {
                float startX = mIntervalLeftRight * i + mIntervalLeft;
                float startY = getHeight()- (maxBrokenLineAvgHeight * mMilliliter.get(i))-mYInterval;
                if (i==mShowIndex){
                    canvas.drawCircle(startX,startY, DisplayUtil.dip2px(mContext, 5f),mPaintCircle);
                }
            }
        }

    }

}

//            // LinearGradient 第一个参数第二个参数为 起始位置x,y  三四参数为终点位置x,y。
//            // 如果x不变则为y轴渐变， y不变则为x轴渐变
//            // 第五个参数为颜色渐变，此处为红色渐变为绿色
//            // 第七个参数为渐变次数，可repeat
//            Shader mShader=new LinearGradient(startX, startY, stopX, stopY,
//                    new int[]{getResources().getColor(R.color.orange_line_start),
//                            getResources().getColor(R.color.orange_line_stop)},
//                    null,Shader.TileMode.CLAMP);
//            mPaintBrokenLine.setShader(mShader);
